package com.msb.mall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.common.dto.SkuReductionDto;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.Query;
import com.msb.mall.coupon.dao.SkuFullReductionDao;
import com.msb.mall.coupon.entity.MemberPriceEntity;
import com.msb.mall.coupon.entity.SkuFullReductionEntity;
import com.msb.mall.coupon.entity.SkuLadderEntity;
import com.msb.mall.coupon.service.MemberPriceService;
import com.msb.mall.coupon.service.SkuFullReductionService;
import com.msb.mall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    MemberPriceService memberPriceService;
    @Autowired
    SkuLadderService skuLadderService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存满减折扣会员价的相关信息
     * @param dto 参数类
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSkuReduction(SkuReductionDto dto) {
        //5.3保存满减，折扣，会员价
        // mall_sms:sms_sku_ladder sms-full_reduction sms_member_price
        //1.折扣
        SkuLadderEntity skuLadder = new SkuLadderEntity();
        skuLadder.setSkuId(dto.getSkuId());
        skuLadder.setFullCount(dto.getFullCount());
        skuLadder.setDiscount(dto.getDiscount());
        skuLadder.setAddOther(dto.getCountStatus());
//        skuLadder.setPrice(0.00);
        if(dto.getFullCount() > 0){
            skuLadderService.save(skuLadder);
        }
        //2.满减
        SkuFullReductionEntity skuFullReduction = new SkuFullReductionEntity();
        BeanUtils.copyProperties(dto,skuFullReduction);
        skuFullReduction.setSkuId(skuFullReduction.getSkuId());
        if(dto.getFullPrice().compareTo(new BigDecimal(0)) > 0){
            this.save(skuFullReduction);
        }
        if(dto.getMemberPrice() !=null && !dto.getMemberPrice().isEmpty()){
            //3.会员价
            List<MemberPriceEntity> memberPrices = dto.getMemberPrice().stream().map((item) -> {
                MemberPriceEntity memberPrice = new MemberPriceEntity();
                memberPrice.setSkuId(dto.getSkuId());
                memberPrice.setMemberLevelId(item.getId());
                memberPrice.setMemberPrice(item.getPrice());
                memberPrice.setMemberLevelName(item.getName());
                //是否叠加
                memberPrice.setAddOther(1);
                return memberPrice;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(memberPrices);
        }
    }

}
